<?php
if(!defined('FROM_INDEX'))exit('Access denined');

require_once ADODB_PATH.'adodb.inc.php';
require_once ROOT_PATH.'include/sql/bae.ds.php';

class DBFunction{
	private static $_instance = null;

	public static function getInstance(){
		if(!self::$_instance instanceof DataSource)
		{
			self::$_instance = new DataSource();
		}
		/*
		if(!self::$_instance instanceof BaeDataSource)
		{
			self::$_instance = new BaeDataSource();
		}*/
		
		return self::$_instance;
	}
}

class DataSource{
	private $connection = null;
	
	public function __construct(){		
		$this->connection = ADONewConnection(DB_TYPE); 
	    $this->connection->Connect(DB_HOST, DB_USER, DB_PWD, DB_NAME);
	    
		if(!$this->connection)
		{
			exit('Connect failed');
		}
		
		$this->connection->SetFetchMode(ADODB_FETCH_ASSOC);

		$this->execute("SET character_set_results = 'utf8', character_set_client = 'binary', character_set_connection = 'utf8'");
	}

	public function setDebug($status=true){
		if($status)
		{
			$this->connection->debug = true;
		}
		else
		{
			$this->connection->debug = false;
		}
	}

	public function getConnection(){
		return $this->connection;
	}

	public function selectLimit($sql, $rows=10, $offset=0, $args=false){
		try
		{
			$resultSet = $this->connection->SelectLimit($sql, $rows, $offset, $args);
			if($resultSet)
			{
				return $resultSet->GetRows();
			}
		}
		catch(exceptions $e)
		{
			$this->logErrorMsg($e);
		}

		return false;
	}

	public function getAll($sql, $args = array()){
		try
		{
			$resultSet = $this->connection->Execute($sql, $args);
			
			if($resultSet)
			{
				return $resultSet->GetRows();
			}
		}
		catch(exceptions $e)
		{
			$this->logErrorMsg($e);
		}

		return false;
	}

	public function getOne($sql, $args = array()){
		try
		{
			return $this->connection->GetOne($sql, $args);
		}
		catch(exceptions $e)
		{
			$this->logErrorMsg($e);
		}

		return false;
	}

	public function getCol($sql, $args = array()){
		try
		{
			return $this->connection->GetCol($sql, $args);
		}
		catch(exceptions $e)
		{
			$this->logErrorMsg($e);
		}

		return false;
	}

	public function getRow($sql, $args = array()){
		try
		{
			$resultSet = $this->connection->Execute($sql, $args);
			
			if($resultSet && $resultSet->RecordCount() > 0)
			{
				return $resultSet->FetchRow();
			}
		}
		catch(exceptions $e)
		{
			$this->logErrorMsg($e);
		}

		return false;
	}

	public function executeTransaction($sql, $args = array()){
		try
		{
			$this->connection->StartTrans();
			$resultSet = $this->connection->Execute($sql, $args);
			$this->connection->CompleteTrans();
			
			if($resultSet)
			{
				return $this->connection->Affected_Rows();
			}
		}
		catch(exceptions $e)
		{
			$conn->RollbackTrans();
			$this->logErrorMsg($e);
		}

		return false;
	}

	public function startTrans(){
		try
		{
			return $this->connection->StartTrans();
		}
		catch(exceptions $e)
		{
			$this->logErrorMsg($e);
		}
		return false;
	}

	public function completeTrans(){
		try
		{
			return $this->connection->CompleteTrans();
		}
		catch(exceptions $e)
		{
			$this->logErrorMsg($e);
		}
		return false;
	}

	public function rollbackTrans(){
		try
		{
			return $this->connection->RollbackTrans();
		}
		catch(exceptions $e)
		{
			$this->logErrorMsg($e);
		}
		return false;
	}

	public function execute($sql, $args = array()){
		try
		{
			$resultSet = $this->connection->Execute($sql, $args);
			
			if($resultSet)
			{
				return $this->connection->Affected_Rows();
			}
		}
		catch(exceptions $e)
		{
			$this->logErrorMsg($e);
		}

		return false;
	}

/*
	public function getResultSet($sql, $args = array()){
		try
		{
			$resultSet = $this->connection->Execute($sql, $args);
			
			if($resultSet)
			{
				return $resultSet;
			}
		}
		catch(exceptions $e)
		{
			$this->logErrorMsg($e);
		}

		return false;
	}
*/

	public function insert($sql, $args = array()){
		try
		{
			$resultSet = $this->connection->Execute($sql, $args);
				
			if($resultSet)
			{
				$insertId = $this->connection->Insert_ID();
				return $insertId;
			}
				
		}
		catch(exceptions $e)
		{
			$this->logErrorMsg($e);
		}
		
		return false;
	}

	public function update($sql, $args = array()){
		try
		{
			return $this->execute($sql, $args);
		}
		catch(exceptions $e)
		{
			$this->logErrorMsg($e);
		}
		return false;
	}

/*
	public function pageExecute($sql, $nrows, $page, $inputarr=false, $secs2cache=0){
		try
		{
			return $this->connection->PageExecute($sql, $nrows, $page, $inputarr, $secs2cache);
		}
		catch(exceptions $e)
		{
			$this->logErrorMsg($e);
		}
		return false;
	}
*/

	public function logErrorMsg($e = null){
		ob_start('db_error');
		
		echo "DB Error, could not query the database\n";
		
		if($e != null)
		{
			var_export($e);
		}
		
		echo $this->connection->ErrorMsg();
		
		$logInfo = ob_get_clean();
		
		sql_log($logInfo);
	}

}